package display;
import function.*;
import exception.*;

import java.util.*;

import controller.IOPrinter;
import dataObject.*;
public class Client {
	private ArrayList<Brand> brandList;
	private static Client site = new Client();
	private ArrayList<Function> functionList;
	private Client()
	{
		brandList = new ArrayList<Brand>();
		functionList = new ArrayList<Function>();
		setUpFunction();
	}
	
	public static Client getSite()
	{
		return site;
	}
	
	public void setUpFunction()
	{
		Function listAllProduct = new ListAllBrandProduct();
		functionList.add(listAllProduct);
		Function suggestion = new Suggestion();
		functionList.add(suggestion);
		Function listSelectedBrand = new ListSelectedBrand();
		functionList.add(listSelectedBrand);
	}
	
	public void setUp(String productString)
	{
		String[] s = productString.split("//");
		setUpBrand(s[0]);
		String brandName = s[0];
		String productName = s[1];
		String releaseDate = s[2];
		double size = Double.parseDouble(s[3]);
		int price = Integer.parseInt(s[4]);
		int batterySize = Integer.parseInt(s[5]);
		int cameraPixel = Integer.parseInt(s[6]);
		int availableColor = Integer.parseInt(s[7]);
		Brand targetbrand = findBrand(brandName);
		targetbrand.setUpProduct(productName,releaseDate,size,price,batterySize,cameraPixel,availableColor);
	}
	
	public void setUpBrand(String brandname)
	{
		Brand targetbrand = findBrand(brandname);
		if(targetbrand==null)
		{
			Brand brand = new Brand(brandname);
			brandList.add(brand);
		}
	}
	
	public Brand findBrand(String brandname)
	{
		for(int i=0;i<brandList.size();i++)
		{
			if(brandList.get(i).getName().equals(brandname))
			{
				return brandList.get(i);
			}
		}
		return null;
	}
	
	public void generateMenu()
	{
		String menuString = "";
		IOPrinter printer = IOPrinter.getPrinter();
		menuString +="Welcome to the Mobile Phone View System!\r\n";
		menuString +="Please select your option in number(eg:1) or 0 to exit.";
		printer.print(menuString);
		int counter=0;
		for(Function f:functionList)
		{
			counter++;
			printer.printMenuFormat(counter,f.getFunctionName());
		}
	}
	
	public void getUserSelection(int userInput) throws UserSelectionOutOfRangeException
	{
		int counter=0;
		if(userInput>functionList.size()||userInput<0)
		{
			throw new UserSelectionOutOfRangeException();
		}
		for(Function f:functionList)
		{	
			counter++;
			if(counter==userInput)
			{
				f.perform(brandList);
			}
		}	
	}
	
	//for testing only
	public ArrayList<Brand> getBrandList()
	{
		return brandList;
	}
	public ArrayList<Function> getFunctionList()
	{
		return functionList;
	}
	public void clearBrandList()
	{
		brandList.clear();
	}
	public void clearFunctionList()
	{
		functionList.clear();
	}
	
}
